package Utils;

import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;

import entity.ConsumInfo;
import entity.MobileCard;
import entity.NetPackage;
import entity.Scene;
import entity.ServicePackage;
import entity.SuperPackage;
import entity.TalkPackage;

public class CardUtil {
	//卡号　此卡的套餐
	Map<String, MobileCard> cards = new HashMap<String, MobileCard>();//卡的　信息要办什么类型的　卡
	//卡号消费记录
	Map<String, LinkedList<ConsumInfo>> consumInfo = new HashMap<String, LinkedList<ConsumInfo>>();
	public CardUtil() {//初始化卡号数据
		cards.put("13932329339", new MobileCard("13932329339", 
				"小明", "11111", new NetPackage(),100));
		cards.put("13932329389", new MobileCard("13932329389", 
				"小红", "11111", new NetPackage(),100));
		add();
	}
	List<Scene> list = new ArrayList<Scene>();
	public void init() {
		list.add(new Scene("通话", 50, "问候客户"));
		list.add(new Scene("短信", 5, "下发通知"));
		list.add(new Scene("流量", 50, "浏览新闻"));
		list.add(new Scene("通话", 30, "普通电话问候"));
		list.add(new Scene("流量", 50, "浏览ｑｑ"));
	}
	public void add() {
		LinkedList<ConsumInfo> list = new LinkedList<ConsumInfo>();
		list.add( new ConsumInfo("13932329389", "发短信", 60));
		consumInfo.put("13932329389",list);
	}
	//是否存在此卡用户
	public MobileCard isExistCard(String number,String password) {
		boolean res = false;
		Set<String> set = cards.keySet();
		for (String string : set) {
			MobileCard key = cards.get(string);
			if(string.equals(number) && key.getPassWord().equals(password))
				return key;
		}
		return null;
	}
	//查看卡号　是否已经注册
	public boolean isExistCard(String serchNumber) {
		return false;
	}
	//生成随机卡号
	public String createNumber() {
		StringBuffer s = new StringBuffer();
		s.append(139);
		Random random = new Random();
		for (int i = 0; i < 8; i++) {
			s.append(random.nextInt(9));
		}
		
		return s.toString();
	}
	//生成每次　随机的　卡号　列表 存入制定次数的　卡号
	public String[] getNewNumbers(int count) {
		String[] c = new String[count];
		for (int i = 0; i < c.length; i++) {
			c[i] = createNumber();//把卡号存入数组之中
		}
		return c;
	}
	//添加新卡
	public void addCard(MobileCard card) {
		cards.put(card.getCardNumber(), card);
		System.out.println("注册成功！手机号为:"+card.getCardNumber()+" 用户名："+card.getUserName()+" 当前剩余余额为"+card.getAonsumAmount());
	}
	//制定卡号　办理退网
	public void delCard(String delNumber) {
		cards.remove(delNumber);
	}
	//查询制定套餐余量
	public void showRemainDetail(String searchNumber) {
		System.out.println("您的手机号为;"+searchNumber);
		System.out.println("套餐剩余查询：");
		Set<String> set = cards.keySet();
		for (String string : set) {
			MobileCard card = cards.get(string);
			if(string.equals(searchNumber)) {
				if(card.getSerPacage() instanceof TalkPackage) {
					System.out.println("\t通话时长"+(((TalkPackage)card.getSerPacage()).getTalkTime()-card.getRealTalkTime()));
					System.out.println("\t短信数量"+(((TalkPackage)card.getSerPacage()).getSmsCount()-card.getRealSMSCount()));
					System.out.println();
				}else if(card.getSerPacage() instanceof NetPackage) {
					System.out.println("\t剩余流量"+(((NetPackage)card.getSerPacage()).getFlow()-card.getRealFlow())/1024+"G");
					System.out.println();
				}else if(card.getSerPacage() instanceof SuperPackage) {
					System.out.println("\t通话时长"+(((SuperPackage)card.getSerPacage()).getTalkTime()-card.getRealTalkTime()));
					System.out.println("\t短信数量"+(((SuperPackage)card.getSerPacage()).getSmsCount()-card.getRealSMSCount()));
					System.out.println("\t剩余流量"+(((SuperPackage)card.getSerPacage()).getFlow()-card.getRealFlow())/1024);
					System.out.println();
				}
			}
		}
	}
	//查寻到月消费详单
	public void showAmountDetail(String searchNumber) {
		LinkedList<ConsumInfo> linkedList = consumInfo.get(searchNumber);
		System.out.println("×××××××××××电话号"+searchNumber+"××××××××××××××××");
		for (ConsumInfo consumInfo : linkedList) {
			System.out.println("\t消费类型"+consumInfo.getType()+"消费数据"+consumInfo.getConsumData());
		}
	}
	//制定卡号　更换套餐
	public void changingPack(String number,String packNum) {
		ServicePackage package1 = cards.get(number).getSerPacage();
		double money = cards.get(number).getAonsumAmount();
		if(money<58) {
			System.out.println("余额不足　请充值");
			return;
		}
		boolean res = true;
		if(packNum.equals("话唠套餐") && !(package1 instanceof TalkPackage)) {
			cards.get(number).setSerPacage(new TalkPackage());
		}else if(packNum.equals("网虫套餐") && !(package1 instanceof NetPackage)) {
			cards.get(number).setSerPacage(new NetPackage());
		}else if(packNum.equals("网虫套餐") && !(package1 instanceof SuperPackage)) {
			cards.get(number).setSerPacage(new SuperPackage());
		}else {
			res = false;
		}
		if(res) {
			System.out.println("办理套餐成功");
		}else {
			System.out.println("已是改套餐　无需更改");
		}
	}
	//为手机卡充值
	public void chargeMoney(String number,double money) {
		cards.get(number).setAonsumAmount(cards.get(number).getAonsumAmount()+money);
		System.out.println("充值成功");
	}
	//添加一条　指定卡的　消费　记录
	//要添加　消费记录　的　卡　要添加的　消费记录
	public void addConsumInfo(String number,ConsumInfo info) {
		Set<String> set = consumInfo.keySet();
		boolean res = false;
		for (String string : set) {
			if(string.equals(number)) {
				res =true;
				consumInfo.get(string).add(info);
			}
		}
		if(res==false) {
			System.out.println("不存在此卡的　消费记录　　已添加一条消费记录");
			consumInfo.put(number, new LinkedList<ConsumInfo>());
			consumInfo.get(number).add(new ConsumInfo(number, info.getType(), info.getConsumData()));
		}
	}
	//打印消费记录
	public void printConsumInfo(String number) throws IOException {
		FileOutputStream fos = new FileOutputStream("资费查询.text");
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
		StringBuffer s = new StringBuffer();
		Set<String> set = consumInfo.keySet();
		for (String string : set) {
			s.append("*************"+string+"*********\r");
			LinkedList<ConsumInfo>list = consumInfo.get(string);
			for (int i = 0; i < list.size(); i++) {
				s.append((i+1)+"\t\t"+list.get(i).getType()+"\t\t"+list.get(i).getConsumData()+"\r");
				
			}
		}
		bw.write(s.toString());
		bw.close();
		fos.close();
	}
	//使用搜搜
	public void userSoso(String number) {
		Random random = new Random();
		int ran= random.nextInt(list.size()-1);
		Scene scene =list.get(ran);
		scene.showInfo();
		addConsumInfo(number, new ConsumInfo(number, scene.getType(), scene.getData()));//添加制定消费记录
		MobileCard card = null;
		card= cards.get(number);
		if(card.getAonsumAmount()>0) {
		if(card.getSerPacage() instanceof TalkPackage) {
			if(scene.getType().equals("通话")) 
				((TalkPackage)card.getSerPacage()).call(scene.getData(), card);
			else if(scene.equals("短信"))
				((TalkPackage)card.getSerPacage()).send(scene.getData(), card);
			else {
				for (int i = 0; i < scene.getData(); i++) {
					card.setAonsumAmount(card.getAonsumAmount()-0.05);
				}
			}	
		}
		if(card.getSerPacage() instanceof SuperPackage) {
			if(scene.getType().equals("通话")) 
				((SuperPackage)card.getSerPacage()).call(scene.getData(), card);
			else if(scene.getType().equals("短信"))
				((SuperPackage)card.getSerPacage()).send(scene.getData(), card);
			else {
				((SuperPackage)card.getSerPacage()).netPlay(scene.getData(), card);
			}
		}
		if(card.getSerPacage() instanceof NetPackage) {
			if(scene.getType().equals("流量")) 
				((NetPackage)card.getSerPacage()).netPlay(scene.getData(), card);

			else if(scene.getType().equals("短信"))
				for (int i = 0; i < scene.getData(); i++) {
					card.setAonsumAmount(card.getAonsumAmount()-0.5);
				}			
			else {
				for (int i = 0; i < scene.getData(); i++) {
					card.setAonsumAmount(card.getAonsumAmount()-0.7);
				}
			}
		}
		}else {
			System.out.println("余额不足　请充值");
		}
	}
	public void susNumber(int num) {
		
	}
	//根据套餐讯号　返回套餐对象
	public ServicePackage createPack(int packId) {
		ServicePackage package1 = null;
		switch (packId) {
		case 1:
			package1 = new TalkPackage();
			break;
		case 2:
			package1 = new NetPackage();
			break;
		case 3:
			package1 = new SuperPackage();
			break;
		}
		
		return package1;
	}
	//显示资费说明
	public void showDescription() {
		
	}
}
 